package com.joplus.housekeeper.ui.view.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.CamboDialogAdapter;
import com.joplus.housekeeper.bean.CamboDetailBean;
import com.joplus.housekeeper.bean.CamboDialogBean;
import com.joplus.housekeeper.utils.DensityUtils;
import com.joplus.housekeeper.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2015/12/25.
 */
public class CamboDialog {


    private Context mContext;


    private AlertDialog dialog;
    private final ImageView iv;
    private final TextView tv;
    private final ListView lv;

    private final ImageView more;

    private boolean isDown = true;
    /*
    false 底
    ture 顶
    */
    private CamboDialogBean bean2;
    private CamboDetailBean bean1;
    private CamboDialogAdapter adapter;
    private final int balanceSum;


    public CamboDialog(final Context context, int i, CamboDetailBean bean, CamboDialogBean beanD) {


        this.mContext = context;
        this.bean2 = beanD;
        this.bean1 = bean;

      /*  AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog_fullscreen);*/
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_cambo, null);

        final Dialog dialog = new Dialog(mContext, R.style.dialog_fullscreen);


        balanceSum = Integer.valueOf(bean1.getStoredDTO().getBalanceCount());
        iv = (ImageView) view.findViewById(R.id.dialog_cambo_iv);
        tv = (TextView) view.findViewById(R.id.dialog_cambo_tv);
        lv = (ListView) view.findViewById(R.id.dialog_cambo_lv);
        more = (ImageView) view.findViewById(R.id.dialog_cambo_iv_more);

        if (isLastItemVisible()) {
            more.setVisibility(View.INVISIBLE);
        } else {
            more.setVisibility(View.VISIBLE);
        }

        if (i == 4) {
            i = bean1.getStoredDTO().getLevelList().size() - 1;
        }

        ImageLoader.getInstance().displayImage(bean1.getStoredDTO().getPriPic(), iv);

        tv.setText(bean1.getStoredDTO().getLevelList().get(i).getLevelName().replace("@@", ""));

        if (StringUtils.isEmpty(bean1.getStoredDTO().getBalanceSum()) || bean1.getStoredDTO().getBalanceSum().equals("0") || balanceSum < bean.getStoredDTO().getLevelList().get(1).getMoney()) {
            //4个等级
            adapter = new CamboDialogAdapter(mContext, bean2, i);
        } else {
            adapter = new CamboDialogAdapter(mContext, bean1);
        }
        lv.setAdapter(adapter);

        view.findViewById(R.id.tip_tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });


        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                if (scrollState == 0) {
                    if (lv.getChildAt(0).getTop() >= 0) {
                        more.setImageResource(R.drawable.downiv);
                        isDown = true;
                    } else if (lv.getLastVisiblePosition() == lv.getCount() - 1) {
                        more.setImageResource(R.drawable.upiv);
                        isDown = false;
                    }


                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        dialog.setContentView(view);
     /*   builder.setView(view);


        dialog = builder.show();*/
        dialog.show();

        int width = DensityUtils.dip2px(context, 300);

        int height = DensityUtils.dip2px(context, 400);
        dialog.getWindow().setLayout(width,
                height);


    }


    private boolean isLastItemVisible() {


        final Adapter adapter1 = lv.getAdapter();

        if (null == adapter || adapter.isEmpty()) {
            return true;
        }

        final int lastItemPosition = adapter.getCount() - 1;
        final int lastVisiblePosition = lv.getLastVisiblePosition();


        if (lastVisiblePosition >= lastItemPosition - 1) {
            final int childIndex = lastVisiblePosition - lv.getFirstVisiblePosition();
            final int childCount = lv.getChildCount();
            final int index = Math.min(childIndex, childCount - 1);
            final View lastVisibleChild = lv.getChildAt(index);
            if (lastVisibleChild != null) {
                return lastVisibleChild.getBottom() <= lv.getBottom();
            }
        }

        return false;
    }

    public void dismiss() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }


}
